/*
 * Copyright 2017 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.wdciti.sccp.iot.springvertx.factory;

import com.wdciti.sccp.iot.common.aio.HandlerDeploy;
import com.wdciti.sccp.iot.springvertx.annotation.VerticleBean;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Promise;
import io.vertx.core.Verticle;
import io.vertx.core.spi.VerticleFactory;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * A {@link VerticleFactory} backed by Spring's {@link ApplicationContext}. It allows to implement verticle as Spring
 * beans and thus benefit from dependency injection, ...etc.
 *
 * @author early
 */
@Slf4j
public class SpringBeanVerticleFactory extends AbstractVerticleFactory implements ApplicationContextAware {

//    private final VerticleExclude exclude;

    private ApplicationContext applicationContext;

    @Override
    public String prefix() {
        return "bean";
    }

    @Override
    public void createVerticle(String verticleName, ClassLoader classLoader, Promise<Callable<Verticle>> promise) {
        String name = VerticleFactory.removePrefix(verticleName);
        //should use @Scope(PROTOTYPE) if instance>1,every call will create new Instance.
        promise.complete(() -> applicationContext.getBean(name, Verticle.class));
    }

    @Override
    public HandlerDeploy prepare() {
        Map<String, Object> verticleObjs = applicationContext.getBeansWithAnnotation(VerticleBean.class);
        for (String verticle : verticleObjs.keySet()) {
            String verticleName = this.verticleFullName(verticle);
            VerticleBean bean = verticleObjs.get(verticle).getClass().getAnnotation(VerticleBean.class);
            DeploymentOptions options = this.getDeploymentOptions(bean);
            this.willDeploy.put(verticleName, options);
        }
        return this;
    }


    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


    private DeploymentOptions getDeploymentOptions(VerticleBean bean) {
        DeploymentOptions options = new DeploymentOptions()
                .setWorker(bean.isWorker())
                .setHa(bean.useHA())
                .setInstances(bean.instances())
                .setWorkerPoolSize(bean.workerPoolSize());
        if (StringUtils.hasText(bean.workerPoolName())) {
            options.setWorkerPoolName(bean.workerPoolName());
        }
        return options;
    }
}
